package com.sky.controller.admin;

import com.ejlchina.searcher.SearchResult;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import com.sky.web.controller.BaseController;
import com.sky.web.domain.AjaxResult;
import com.sky.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(tags = "管理端-套餐浏览接口")
@Slf4j
@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
public class SetmealController extends BaseController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 分页条件查询
     *
     * @param dto 套餐信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    public TableDataInfo list(SetmealDTO dto) {
        SearchResult<SetmealVO> list = setmealService.selectSetmeal(dto);
        return getDataTable(list);
    }

    /**
     * 根据套餐id查询套餐详情
     *
     * @param id 套餐id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据套餐id查询套餐详情")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        SetmealVO setmeal = setmealService.selectSetmealById(id);
        return AjaxResult.success(setmeal);
    }

    /**
     * 新增套餐
     * @param dto 套餐信息
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#dto.categoryId")
    public AjaxResult save(@RequestBody SetmealDTO dto){
        log.info("新增套餐：{}", dto);
        return toAjax(setmealService.insertSetmeal(dto));
    }


    /**
     * 修改套餐状态
     * @param dto 套餐信息
     * @return
     */
    @PutMapping("/updateStatus")
    @ApiOperation("修改套餐状态")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public AjaxResult updateStatus(@RequestBody SetmealDTO dto){
        return toAjax(setmealService.updateSetmealStatus(dto));
    }

    /**
     * 修改套餐
     * @param dto 套餐信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public AjaxResult update(@RequestBody SetmealDTO dto){
        return toAjax(setmealService.updateSetmeal(dto));
    }


    /**
     * 删除套餐
     * @param ids id集合
     * @return
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public AjaxResult deleteById(@PathVariable Long[] ids){
        log.info("删除套餐：{}", Arrays.asList(ids));
        return toAjax(setmealService.deleteSetmealByIds(ids));
    }
}
